package com.yuke.cloud.service.uac.controller.frontend;

import com.yuke.cloud.common.base.dto.LoginAuthDto;
import com.yuke.cloud.common.core.annotation.LogAnnotation;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.uac.dto.RoleBindMenuDto;
import com.yuke.cloud.service.uac.entity.AccRoleMenu;
import com.yuke.cloud.service.uac.service.AccRoleMenuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.service.uac.vo.BindAuthVo;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import com.yuke.cloud.common.core.support.BaseController;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by wg on 2018/10/31.
*/
@Api(value = "WEB - AccRoleMenuController", tags = "处理AccRoleMenu相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/acc/role_menu")
public class AccRoleMenuController extends BaseController {
    @Resource
    private AccRoleMenuService accRoleMenuService;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "accRoleMenu", value = "该数据记录的POJO") @RequestBody AccRoleMenu accRoleMenu) {
        accRoleMenuService.save(accRoleMenu);
        return WrapMapper.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        accRoleMenuService.deleteById(id);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "accRoleMenu", value = "需要修改的内容") @RequestBody AccRoleMenu accRoleMenu) {
        accRoleMenuService.update(accRoleMenu);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<AccRoleMenu> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        AccRoleMenu accRoleMenu = accRoleMenuService.findById(id);
        return WrapMapper.ok(accRoleMenu);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<AccRoleMenu>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<AccRoleMenu> list = accRoleMenuService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    /**
     * 获取菜单树.
     *
     * @param roleId the role id
     *
     * @return the menu tree by role id
     */
    @GetMapping(value = "/getMenuTreeByRoleId/{roleId}")
    @ApiOperation(httpMethod = "GET", value = "获取菜单树")
    public Wrapper<BindAuthVo> getMenuTreeByRoleId(@ApiParam(name = "roleId", value = "角色id") @PathVariable Long roleId) {
        logger.info("roleId={}", roleId);
        BindAuthVo bindAuthVo = accRoleMenuService.getMenuTreeByRoleId(roleId);
        return WrapMapper.ok(bindAuthVo);
    }

    /**
     * 角色分配菜单.
     *
     * @param roleBindMenuDto the role bind menu dto
     *
     * @return the wrapper
     */
    @PostMapping(value = "/bindMenu")
    @ApiOperation(httpMethod = "POST", value = "角色分配菜单")
    @LogAnnotation
    public Wrapper bindMenu(@ApiParam(name = "bindAuth", value = "权限信息") @RequestBody RoleBindMenuDto roleBindMenuDto) {
        logger.info("角色分配权限. roleBindMenuDto= {}", roleBindMenuDto);
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        accRoleMenuService.bindMenu(roleBindMenuDto, loginAuthDto);
        return WrapMapper.ok();
    }
}
